import tkinter as tk
from tkinter import filedialog
from config import Config
from audio_extractor import extract_audio
from transcriber import transcribe_to_srt

def main():
    root = tk.Tk()
    root.title("视频字幕生成工具")
    root.geometry("400x300")
    root.resizable(True, True)

    config = Config()

    def select_file():
        file_path = filedialog.askopenfilename(
            title="选择视频文件",
            filetypes=[("视频文件", "*.mp4 *.avi *.mov *.mkv")]
        )
        if file_path:
            status_label.config(text="处理中...")
            try:
                audio_path = extract_audio(file_path)
                srt_path = transcribe_to_srt(audio_path, config)
                status_label.config(text=f"处理完成！保存于：{srt_path}")
            except Exception as e:
                status_label.config(text=f"错误：{str(e)}")

    # 使用网格布局管理器
    main_frame = tk.Frame(root, padx=20, pady=20)
    main_frame.pack(expand=True, fill='both')

    tk.Label(main_frame, text="模型大小：").grid(row=0, column=0, sticky='w', pady=(0, 10))
    model_var = tk.StringVar(value=config.model_size)
    def update_language(*args):
        if model_var.get() == 'tiny':
            lang_var.set('en')
        else:
            lang_var.set(config.target_language)
    model_var.trace('w', update_language)
    model_menu = tk.OptionMenu(main_frame, model_var, "tiny", "base", "small", "medium", "large")
    model_menu.grid(row=0, column=1, sticky='ew', pady=(0, 10))

    tk.Label(main_frame, text="目标语言：").grid(row=1, column=0, sticky='w', pady=(0, 10))
    lang_var = tk.StringVar(value=config.target_language)
    lang_menu = tk.OptionMenu(main_frame, lang_var, "zh", "en", "ja", "fr", "de")
    lang_menu.grid(row=1, column=1, sticky='ew', pady=(0, 10))

    tk.Button(main_frame, text="选择视频文件", command=select_file).grid(row=2, columnspan=2, sticky='ew', pady=(10, 0))
    status_label = tk.Label(main_frame, text="")
    status_label.grid(row=3, columnspan=2, sticky='ew', pady=(10, 0))

    # 配置网格列权重
    main_frame.columnconfigure(0, weight=1)
    main_frame.columnconfigure(1, weight=3)

    root.mainloop()

if __name__ == "__main__":
    main()